//
//  ShopStateController.m
//  DMGME
//
//  Created by ihope99 on 15/9/8.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "ShopStateController.h"

@interface ShopStateController ()
{
    NSString *_yesOrNo;
    NSString *_stateStr;
    NSString *_stateStr1;
    
}
@end

@implementation ShopStateController
-(void)viewWillAppear:(BOOL)animated
{
    [self aboutStateResquest];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品状态设置";
    self.view.backgroundColor = [UIColor whiteColor];
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    
    _comfirmGoodBtn.hidden = YES;
    _emptyGoodBtn.hidden = YES;
    [_comfirmGoodBtn setBackgroundImage:[UIImage imageNamed:@"ConfirmGoodsButtonGray"] forState:UIControlStateNormal];
    [_comfirmGoodBtn setBackgroundImage:[UIImage imageNamed:@"ConfirmGoodsButton"] forState:UIControlStateSelected];
    [_emptyGoodBtn setBackgroundImage:[UIImage imageNamed:@"EmptyGoodsButton1"] forState:UIControlStateSelected];
    [_emptyGoodBtn setBackgroundImage:[UIImage imageNamed:@"EmptyGoodsButtonGray"] forState:UIControlStateNormal];
    
    [_comfirmGoodBtn addTarget:self action:@selector(comfirGoodBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_emptyGoodBtn addTarget:self action:@selector(emtypGoodBtnClick) forControlEvents:UIControlEventTouchUpInside];
//      NSLog(@"status == %d",[_yesOrNo intValue]);

}

- (void)comfirGoodBtnClick{
    
    if ([_yesOrNo  isEqual: @"1"]||[_stateStr  isEqual: @"1"]) {
        
        [self showMsg:@"该商品已上架"];
    }else
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
       [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$added"] forKey:@"token"];
        [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
        [dict setValue: @"added" forKey:@"action"];
        [dict setValue: _yesOrNo forKey:@"status"];
        NSString *url = nil;
        if (_type == 2) {
            url = BookManageperipheryNET;
      
            [dict setValue: _periodInfoId forKey:@"periphery_id"];
        }
        else
        {
            url = BookManagePeriodNET;
            [dict setValue: _periodInfoId forKey:@"period_id"];
        }
        [self StartLoading];
        
        [AFDownManager POST:url parameters:dict success:^(id responseObject) {
//            NSLog(@"dict == %@",dict);
//            NSLog(@"url == %@",BookManagePeriodNET);
//            NSLog(@"商品状态设置 == %@",responseObject);
            [self StopLoading];
            if ([responseObject[@"data"] count] == 0) {
             
            }else
            {
            if (responseObject[@"data"][@"reslut"]) {
//                [self showMsg:@"成功上架"];

                _comfirmGoodBtn.selected = NO;

                _emptyGoodBtn.selected = YES;
                _stateStr1 = @"0";
                 _stateStr = @"1";
                
                [self aboutStateResquest];

            }
            }
           
        } failure:^(NSError *error) {
            [self StopLoading];

        }];
    }
}

//delete下架
- (void)emtypGoodBtnClick
{
//    NSLog(@"=== %@",_strEnter);
    if([_yesOrNo  isEqualToString: @"0"]||[_stateStr1  isEqual: @"1"]||[_yesOrNo  isEqualToString: @"-1"])
    {
        [self showMsg:@"无可下架期刊"];
      
    }
    else
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$delete"] forKey:@"token"];
        [dict setValue:_yesOrNo forKey:@"status"];
        [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
        [dict setValue: @"delete" forKey:@"action"];
        NSString *url = nil;
        if (_type == 2) {
            url = OffShelf1;
            [dict setValue: _periodInfoId forKey:@"periphery_id"];
        }
        else{
            url = OffShelf;
            [dict setValue: _periodInfoId forKey:@"period_id"];
        }
        [self StartLoading];
        [AFDownManager POST:url parameters:dict success:^(id responseObject) {
            [self StopLoading];
//            NSLog(@"url== %@",url);
//            NSLog(@"dict === %@",dict);
//            NSLog(@"%@",responseObject);
            
            if ([responseObject[@"code"] intValue]==0) {
//                [self showMsg:@"下架成功！"];
                _stateStr1 = @"1";
                _stateStr = @"0";

                _emptyGoodBtn.selected = NO;
                _comfirmGoodBtn.selected = YES;
                
                [self aboutStateResquest];
            }
            
//            NSLog(@"%@",responseObject[@"desc"]);
            
        } failure:^(NSError *error) {
            [self StopLoading];
        }];
  
    }
}

-(void)aboutStateResquest
{
    
    UILabel *tishi = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, KWidth-60, 21)];
    
    tishi.textColor = [UIColor grayColor];
    tishi.textAlignment = NSTextAlignmentCenter;
    tishi.text = @"无网络连接......";
    tishi.hidden = YES;
    [self.view addSubview:tishi];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
   
//    NSLog(@"%@",dict);
    NSString *url = nil;
    if (_type == 2) {
        url = BookManageperipheryNET;
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$getPeriphery"] forKey:@"token"];
        [dict setValue: @"getPeriphery" forKey:@"action"];
        [dict setValue: _periodInfoId forKey:@"periphery_id"];
    }
    else{
        url = BookManagePeriodNET;
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$getPeriod"] forKey:@"token"];
        [dict setValue: @"getPeriod" forKey:@"action"];
        [dict setValue: _periodInfoId forKey:@"period_id"];
    }
    [self StartLoading];
    [AFDownManager POST:url parameters:dict success:^(id responseObject) {
        [self StopLoading];
        tishi.hidden = YES;
//        NSLog(@"是否上架 === %@",responseObject[@"data"][@"status"]);
//        NSLog(@"responseObject == %@",responseObject[@"desc"]);
        _yesOrNo = responseObject[@"data"][@"status"];
        if ([responseObject[@"data"][@"status"]  isEqual: @"1"]) {
            _comfirmGoodBtn.selected = NO;
            _emptyGoodBtn.selected = YES;
        }
        if ([responseObject[@"data"][@"status"]  isEqual: @"0"]) {
            _emptyGoodBtn.selected = NO;
            _comfirmGoodBtn.selected = YES;
        }
        if ([responseObject[@"data"][@"status"]  isEqual: @"-1"]) {
            _comfirmGoodBtn.selected = YES;
            _emptyGoodBtn.selected = NO;
        }
        _comfirmGoodBtn.hidden = NO;
        _emptyGoodBtn.hidden = NO;
           } failure:^(NSError *error) {
        [self StopLoading];
               tishi.hidden = NO;
//        NSLog(@"error == %@",error);
               

    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
